package com.attendance.www.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.RetroactiveBean;
import com.attendance.www.hr.beans.RetroactiveTimesBean;
import com.attendance.www.hr.beans.UserEmployeeBean;
import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IAttendanceService;
import com.attendance.www.hr.service.IRetroactiveTimesService;
import com.zhangkongli.frame.core.ResultMap;

/**
 * 补签次数设置
 * @author 张鑫
 * 2018-03-24
 *
 */
@Controller
@RequestMapping("/retroactiveTimes")
public class RetroactiveTimesController {
	
	@Resource
	private IRetroactiveTimesService retroactiveTimesService;
	@Resource
	private IAttendanceService attendanceService;
	
	/**
	 * 增加
	 * @param request
	 * @param retroactiveTimesBean
	 * @return
	 */
	@RequestMapping(value = "/saveRetroactiveTimes.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object saveRetroactiveTimes(HttpServletRequest request, @RequestBody RetroactiveTimesBean retroactiveTimesBean) {
		if (retroactiveTimesBean.getMonth() == null || retroactiveTimesBean.getYear()== null
				|| retroactiveTimesBean.getTimes() == null) {
			throw new ResultFaultException("0_00001");
		}
		return ResultMap.success(retroactiveTimesService.saveRetroactiveTimes(retroactiveTimesBean));
	}
	
	/**
	 * 删
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deleteRetroactiveTimes.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object deleteRetroactiveTimes(HttpServletRequest request) {
		String retroactiveTimesIdStr = request.getParameter("retroactiveTimesId");
		if (StringUtils.isBlank(retroactiveTimesIdStr)) {
			throw new ResultFaultException("0_00001");
		}
		return ResultMap.success(retroactiveTimesService.deleteRetroactiveTimes(retroactiveTimesIdStr));
	}
	
	/**
	 * 改
	 * @param request
	 * @param retroactiveTimesBean
	 * @return
	 */
	@RequestMapping(value = "/updateRetroactiveTimes.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object updateRetroactiveTimes(HttpServletRequest request, @RequestBody RetroactiveTimesBean retroactiveTimesBean) {
		if (retroactiveTimesBean.getRetroactiveTimesId() == null 
				|| retroactiveTimesBean.getMonth() == null 
				|| retroactiveTimesBean.getYear()== null
				|| retroactiveTimesBean.getTimes() == null) {
			throw new ResultFaultException("0_00001");
		}
		return ResultMap.success(retroactiveTimesService.updateRetroactiveTimes(retroactiveTimesBean));
	}
	
	/**
	 * 查
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getRetroactiveTimesList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getRetroactiveTimesList(HttpServletRequest request) {
		return ResultMap.success(retroactiveTimesService.getRetroactiveTimesList());
	}
	
	/**
	 * 查询某时间是否可以补签
	 * @param request
	 * @param retroactiveTimesBean
	 * @return
	 */
	@RequestMapping(value = "/getIsAbleRetroactive.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getIsAbleRetroactive(HttpServletRequest request, @RequestBody RetroactiveBean retroactiveBean) {
		//获取登陆用户数据  
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		retroactiveBean.setEmployeeId(userEmployee.getEmployeeId());
		return ResultMap.success(retroactiveTimesService.getIsAbleRetroactive(retroactiveBean));
	}

}
